# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

from dp_crawler.utils.mysql_connect_util.list_page_util import ListPageMysqlConnectUtil
from dp_crawler.utils.mysql_connect_util.list_shop_info_util import ListShopInfoMysqlConnectUtil
from dp_crawler.utils.mysql_connect_util.detail_shop_info_util import DetailShopInfoMysqlConnectUtil
from dp_crawler.utils.mysql_connect_util.shop_comment_util import ShopCommentMysqlConnectUtil
from dp_crawler.utils.mysql_connect_util.bj_shop_comment_util import BJShopCommentMysqlConnectUtil


class DpCrawlerPipeline(object):
    def __init__(self):
        self.mapping = {
            "dp_list_page_spider": ListPageMysqlConnectUtil,
            "list_page_shop_info_spider": ListShopInfoMysqlConnectUtil,
            "detail_page_shop_info_spider": DetailShopInfoMysqlConnectUtil,
            "shop_new_comment_spider": ShopCommentMysqlConnectUtil,
            "bj_shop_new_comment_spider": BJShopCommentMysqlConnectUtil
        }

    def process_item(self, item, spider):
        mysql_connection = self.mapping.get(str(spider))

        try:
            self.data_2_mysql(mysql_connection, item)
        except Exception as _:
            self.data_2_mysql(mysql_connection, item)
        return item

    @staticmethod
    def data_2_mysql(mysql_connection, item):
        with mysql_connection() as (connection, cursor, _, sql):
            try:
                cursor.execute(sql.format(**dict(item)))
                connection.commit()
            except Exception as e:
                if 'Duplicate' not in str(e):
                    print(e)
                connection.rollback()
